﻿using Gl_mes.DAL.CustomDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Gl_mes.Tools.DataGirdViewExt
{
    public class DataGridViewUILightColumn : DataGridViewColumn
    {
        private BreadcrumbModel breadcrumbModel = new BreadcrumbModel();

        public BreadcrumbModel BreadcrumbModel
        {
            get
            {
                return breadcrumbModel;
            }
            set
            {
                breadcrumbModel = value;
            }
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        public DataGridViewUILightColumn()
        {
            this.CellTemplate = new DataGridViewUILightCell();
        }
    }


    public class DataGridViewUILightCell : DataGridViewCell
    {
        private List<LightModel> lights = new List<LightModel>();

        public List<LightModel> Lights
        {
            get
            {
                return lights;
            }
            set
            {
                lights = value;
            }
        }

        public DataGridViewUILightCell()
        {
            ValueType = typeof(List<LightModel>);
        }


        public override Type ValueType
        {
            get
            {
                return base.ValueType;
            }
            set
            {
                if (value != null && value != typeof(List<LightModel>))
                {
                    throw new ArgumentException("ValueType must be List<LightModel>");
                }

                base.ValueType = value;
            }
        }


        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                                        DataGridViewElementStates elementState, object value, object formattedValue, string errorText,
                                        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (value != null && value is List<LightModel>)
            {
                List<LightModel> tmpList = (List<LightModel>)value;
                if (tmpList != null && tmpList.Count > 0)
                {
                    UIPanel panel = new UIPanel();
                    panel.BackColor = cellStyle.BackColor;
                    panel.Size = new Size(cellBounds.Width - 1, cellBounds.Height - 1);
                    panel.Location = new Point(cellBounds.X, cellBounds.Y);

                    foreach (LightModel tmpModel in tmpList)
                    {
                        UILight tmpLight = new UILight();
                        EntityTools.CopyEntity<LightModel, UILight>(tmpModel, ref tmpLight);
                        if (tmpModel.State.Equals("On"))
                        {
                            tmpLight.State = UILightState.On;
                        }
                        if (tmpModel.State.Equals("Off"))
                        {
                            tmpLight.State = UILightState.Off;
                        }
                        else
                        {
                            tmpLight.State = UILightState.Blink;
                        }
                        panel.Controls.Add(tmpLight);
                    }

                    var bmp = new Bitmap(cellBounds.Width, cellBounds.Height);
                    var g = Graphics.FromImage(bmp);
                    panel.DrawToBitmap(bmp, panel.ClientRectangle);
                    graphics.DrawImage(bmp, cellBounds.Location);

                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,
                            errorText, cellStyle, advancedBorderStyle, paintParts);
                }
            }
        }
    }
}
